#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "en2ch.h"

t_enWordItem g_AllEnWords[ALL_WORDS_NUM];
t_wordExplanation g_AllExplanation[ALL_EXPLANATION_NUM];

int initLib(u8 libType)
{
	u16 lineLen = 0;
	u32 size = (libType == LIB_TYPE_WORDS?ALL_WORDS_NUM:ALL_EXPLANATION_NUM);
	u32 count = 0;
	FILE *fp = NULL;
	char lineBuf[MAX_EXPLANATION_LEN+1];

	if (libType == LIB_TYPE_WORDS)
	{
		fp = fopen(LIB_PATH_EN_WORDS, "r");
	} 
	else if (libType == LIB_TYPE_CN_EXPLANATION)
	{
		fp = fopen(LIB_PATH_CN_EXPLANATION, "r");
	} 
	else
	{
		fp = fopen(LIB_PATH_EN_EXPLANATION, "r");
	}
	
	if(NULL == fp)
	{
		printf("Open file %s failed\n", libType==LIB_TYPE_WORDS?LIB_PATH_EN_WORDS: \
			(libType==LIB_TYPE_CN_EXPLANATION?LIB_PATH_CN_EXPLANATION:LIB_PATH_EN_EXPLANATION));
		return -1;
	}
	while(fgets(lineBuf, MAX_EXPLANATION_LEN, fp))
	{
		if(lineBuf[0] != '"')
		{
			continue;
		}
		
		lineLen = strlen(lineBuf)-3;
		lineBuf[lineLen] = '\0';
		strlwr(lineBuf);

		if(libType == LIB_TYPE_WORDS)
		{
			g_AllEnWords[count].word = (char *)malloc(lineLen+1);
			strncpy(g_AllEnWords[count].word, lineBuf+1, lineLen);
		}
		else
		{
			g_AllExplanation[count].explanation = (char *)malloc(lineLen+1);
			strncpy(g_AllExplanation[count].explanation, lineBuf+1, lineLen);
		}
		count++;
		if (count > size)
		{
			printf("The end of array\n");
			break;
		}
		//printf("%s", lineBuf);
	}
	fclose(fp);
	
	return 0;
}

int cleanLib(u8 libType)
{
	u32 i = 0;
	u32 size = (libType == LIB_TYPE_WORDS?ALL_WORDS_NUM:ALL_EXPLANATION_NUM);

	for (i=0; i<size; i++)
	{
		if(libType == LIB_TYPE_WORDS)
		{
			if(NULL != g_AllEnWords[i].word)
			{
				free(g_AllEnWords[i].word);
				g_AllEnWords[i].word = NULL;
			}
		}
		else
		{
			if(NULL != g_AllExplanation[i].explanation)
			{
				free(g_AllExplanation[i].explanation);
				g_AllExplanation[i].explanation = NULL;
			}
		}
	}

	return 0;
}

int findInLib(const char *word)
{
	char firstChar;
	u16 lowIndex =0, midIndex = 0, highIndex = 0;
	u16 i = 0;

	if (NULL == word)
	{
		return 0;
	}

	firstChar = word[0];
	lowIndex = g_firstCharOffset[firstChar-'a'].offset;
	highIndex = g_firstCharOffset[firstChar-'a'+1].offset;
	
	while( lowIndex <= highIndex)
	{
		midIndex = (lowIndex + highIndex)/2;
		if (strcmp(word, g_AllEnWords[midIndex].word) == 0)
		{
			printf("%s:\n", word);
			printf("%s\n", g_AllExplanation[midIndex].explanation);
			return 1;
		}
		else if (strcmp(word, g_AllEnWords[midIndex].word) < 0)
		{
			highIndex = midIndex - 1;
		}
		else
		{
			lowIndex = midIndex + 1;
		}
	}

	return 0;
}

int main()
{
	u8 ret = 0;
	u32 wordsNum = ALL_WORDS_NUM;
	u32 explanationNun = ALL_EXPLANATION_NUM;
	char buf[MAX_WORD_LEN+1];

	memset(g_AllEnWords, 0x00, sizeof(g_AllEnWords));
	memset(g_AllExplanation, 0x00, sizeof(g_AllExplanation));

	ret = initLib(LIB_TYPE_WORDS);
	if (ret)
	{
		return -1;
	}
	ret = initLib(LIB_TYPE_CN_EXPLANATION);
	if (ret)
	{
		return -1;
	}

	if ( (wordsNum != explanationNun) || (wordsNum != g_firstCharOffset[ARRAY_SIZE(g_firstCharOffset)-1].offset))
	{
		printf("Array size different, please check!\n");
		return -1;
	}

	while(1)
	{
		printf("Please input the word you want to translate! ESC to exit\n");
		scanf("%s", buf);

		if (stricmp(buf, "ESC") == 0)
		{
			break;
		}
		
		if(findInLib(buf) != 1)
		{
			printf("Not find\n");
		}
	}

	cleanLib(LIB_TYPE_WORDS);
	cleanLib(LIB_TYPE_CN_EXPLANATION);

	return 0;
}